from fastapi import UploadFile, File

from doc.parser_doc import DocParser
from doc.parser_ofd import OfdParser
from doc.parser_pdf import PdfParser
from doc.parser_xls import XlsParser
from img.ocr_img import ImgParser
from server import app, success, error

serv = app

@app.post("/parse/any")
async def parse_any(file: UploadFile = File(...)):
    parsers = [
        ImgParser(),
        DocParser(),
        PdfParser(),
        XlsParser(),
        OfdParser()
    ]
    for parser in parsers:
        if parser.can_parse(file.filename):
            result = await parser.parse_upload_file_to_text(file)
            return success(result)
    return error(400, message="Invalid File")

if __name__ == "__main__":
    # 监听特定的IP地址，例如192.168.1.100
    import uvicorn
    uvicorn.run(app="main:serv", host="0.0.0.0", port=8000, workers=1)
